package com.example.maze;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

//activity for the leader board page
public class LeaderBoardActivity extends Activity implements OnClickListener{
	
	//button for player if they finish their game, 
	//see their score in leader board and want to play again
	private Button playAgainButton;
	//to display scores
	private ListView scores;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//for the layout of the page
		setContentView(R.layout.score_board);
		
		LeaderBoardDataSource leaderBoardData = new LeaderBoardDataSource(this);
		leaderBoardData.open();
		
		//setting the amount of points the player needs to achieve to get each medal
		List<PlayerScore> goldLeaderBoard = leaderBoardData.getLeaderBoard(100, 10000);
		List<PlayerScore> silverLeaderBoard = leaderBoardData.getLeaderBoard(20, 100);
		List<PlayerScore> bronzeLeaderBoard = leaderBoardData.getLeaderBoard(0, 20);
		leaderBoardData.close();
		//making the button clickable
		playAgainButton = (Button) findViewById(R.id.playAgainButton);
		playAgainButton.setOnClickListener(this);
		
		//setting a list view for the scores
		scores = (ListView) findViewById(R.id.scores);
		
		//making an adapter for the list and splitting the scores into the 3 medal categories
		//going via mAdapter to mData
		MyCustomAdapter mAdapter = new MyCustomAdapter();
		mAdapter.addItem("Gold:");
		mAdapter.addItem(Integer.valueOf(0));
		mAdapter.addAllItems(goldLeaderBoard);
		mAdapter.addItem("Silver:");
		mAdapter.addItem(Integer.valueOf(0));
		mAdapter.addAllItems(silverLeaderBoard);
		mAdapter.addItem("Bronze:");
		mAdapter.addItem(Integer.valueOf(0));
		mAdapter.addAllItems(bronzeLeaderBoard);
		
        //setting the adapter to the list view
        scores.setAdapter(mAdapter);
		
	}
	
	@Override
	public void onClick(View arg0) {
		//linking the leader board page to the start up page 
		//so when clicked the play again button will take the player to the beginning
		Intent i = new Intent(this, StartUpActivity.class);
		startActivity(i);
	
	}
	//inheriting adapter methods to go between the leader board and data on the leader board
	 private class MyCustomAdapter extends BaseAdapter {
		 	//person
	        private static final int TYPE_ITEM = 0;
	        //medal
	        private static final int TYPE_SEPARATOR = 1;
	        //column name
	        private static final int TYPE_HEADER = 2;
	        private static final int TYPE_MAX_COUNT = 3;

	        //where we store all the data
	        private ArrayList<Object> mData = new ArrayList<Object>();
	        //converts xml items and headers to java
	        private LayoutInflater mInflater;
	        //constructor means we will want to create an instance of it
	        public MyCustomAdapter() {
	            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        }
	        //adding an item to mData
	        public void addItem(Object item) {
	            mData.add(item);
	            notifyDataSetChanged();
	        }
	        //adds whole list to mData to tidy the code instead of having it three times 
	        public void addAllItems(List<PlayerScore> items) {
	        	for(PlayerScore item: items){
	        		mData.add(item);
	        	}
	            notifyDataSetChanged();
	        }
	        
	        @Override
	        public int getItemViewType(int position) {
	        	//overriding the view type as have 3 instead of 1, 
	        	//will be used later to find what view is needed at which position
	        	if(mData.get(position) instanceof String){
	        		return TYPE_SEPARATOR;
	        	}
	        	if(mData.get(position) instanceof Integer){
	        		return TYPE_HEADER;
	        	}
	        	return TYPE_ITEM;
	        }

	        @Override
	        public int getViewTypeCount() {
	        	//counting how many types there are
	            return TYPE_MAX_COUNT;
	        }

	        public int getCount() {
	            return mData.size();
	        }

	        public Object getItem(int position) {
	            return mData.get(position);
	        }

	        public long getItemId(int position) {
	        	//using the position of the item as its id
	            return position;
	        }

	        public View getView(int position, View convertView, ViewGroup parent) {

	            int type = getItemViewType(position);
	            	
                switch (type) {
                    case TYPE_ITEM:
                    	PlayerScore playerScore = (PlayerScore) mData.get(position);
                    	//converting the view to java so can get, convert and change text
                        convertView = mInflater.inflate(R.layout.score_item, null);
                        TextView name = (TextView)convertView.findViewById(R.id.name);
                        TextView size = (TextView)convertView.findViewById(R.id.size);
                        TextView percent = (TextView)convertView.findViewById(R.id.percent);
                        TextView moveTime = (TextView)convertView.findViewById(R.id.moveTime);
                        name.setText(playerScore.getName());
                        size.setText(playerScore.getScore().getSize()+"");
                        percent.setText(playerScore.getScore().getPercent()+ "");
                        moveTime.setText(((int)playerScore.getScore().getScore())+ "");
                        break;
                    case TYPE_HEADER:
                    	//making sure everything is displayed by the convert view
                    	convertView = mInflater.inflate(R.layout.score_header, null);
                    	break;
                    case TYPE_SEPARATOR:
                    	//simple version of type_item
                    	String header = (String) mData.get(position);
                        convertView = mInflater.inflate(R.layout.header, null);
                        TextView top = (TextView)convertView.findViewById(R.id.medal);
                        top.setText(header);
                        break;
                }
	            
	            return convertView;
	        }

	    }
}
